<?php

namespace app\api\controller;

use app\admin\model\Specialtopictype;
use app\admin\model\Specialtopiccontent;
use app\admin\model\Articlecomment;
use app\admin\model\Articlelike;
use app\admin\model\Operator;
use app\admin\model\Image;

class Specialtopic extends BaseController
{

    /**
     * 获取专题分类
     */
    public function getSpecialtopicType()
    {
        $param = self::getHttpParam();

        if (empty($param->specialtopicId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'specialtopicId 不能为空');
        }

        $where = [
            'specialtopicId' => $param->specialtopicId,
        ];

        $result = Specialtopictype::where($where)->field('id as typeId,name')->select();
        if (empty($result)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到专题分类');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 专题内容列表
     */
    public function getSpecialtopiccontentList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->specialtopicId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'specialtopicId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }
        $where = [
            'specialtopicId' => $param->specialtopicId,
        ];
        if(!empty($param->typeId)){
            $where['typeId'] = $param->typeId;
        }

        $model = db('specialtopiccontent');
        $fields = 'id, title, imgUrl, createDate, createOper, specialtopicId, typeId';
        $order = 'createDate DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = Specialtopiccontent::where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关专题内容');
        }

        foreach($result as &$item){
            $memberId = Operator::where(['id' => $item['createOper']])->value('memberId');
            if($memberId && $memberId != 0){
                // 组织
                $organization = Member::alias('a')->where(['a.id' => $memberId])->join('__ORGANIZATION__ o', 'a.organizationId = o.id')->value('o.name');
                $item['organization'] = $organization;
            }else{
                $item['organization'] = $this->defaultOrganization;
            }
            unset($item['createOper']);

            $item['typeName'] = Specialtopictype::where(['id' => $item['typeId']])->field('name')->value('name');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 专题内容详情
     */
    public function getSpecialtopiccontentDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->specialtopiccontentId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'specialtopiccontentId 不能这空');
        }
        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }

        $id = $param->specialtopiccontentId;
        $userId = $param->userId;
        $where = [
            'id' => $id
        ];
        $model = db('specialtopiccontent');
        $fields = 'id, title, imgUrl, createDate, createOper, content, readCount';
        $item = $model->where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关专题内容');
        }

        $memberId = Operator::where(['id' => $item['createOper']])->value('memberId');
        if($memberId && $memberId != 0){
            // 组织
            $organization = Member::alias('a')->where(['a.id' => $memberId])->join('__ORGANIZATION__ o', 'a.organizationId = o.id')->value('o.name');
            $item['organization'] = $organization;
        }else{
            $item['organization'] = $this->defaultOrganization;
        }
        unset($item['createOper']);

        // 每查看一次，阅读数量加1
        Specialtopiccontent::where('id', $id)->setInc('readCount');
        $item['readCount'] += 1;
        // 评论数
        $item['commentCount'] = Articlecomment::where('articleId', $id)->count();
        // 点赞数
        $item['likeCount'] = Articlelike::where('articleId', $id)->count();

        // 是否已点赞
        $isLike = Articlelike::where(['userId' => $userId, 'articleId' => $id])->find();
        if($isLike){
            $isLike = 'true';
        }else{
            $isLike = 'false';
        }
        $item['isLike'] = $isLike;
        // 是否已收藏
        /*$collectWhere = [
            'userId' => $userId,
            'collectId' => $id,
            'type' => 1
        ];
        $collectInfo = action('api/Common/getCollectInfo', ['where' => $collectWhere]);
        $isCollect = 2;
        if($collectInfo){
            $isCollect = 1;
        }
        $item['isCollect'] = $isCollect;*/

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

}